﻿using System;
using System.Collections.Generic;
using QFramework;
using UnityEngine;
using Random = UnityEngine.Random;

namespace ProjectMyFarmAndShe
{
    public enum SeedStates
    {
        Seed,
        Small,
        Mid,
        Big,
        Ripe
    }

    [Serializable]
    public class PlantData
    {
        public string Key;
        public List<PlantState> PlantStates = new List<PlantState>();
    }

    [Serializable]
    public class PlantState
    {
        public int NextStateDay;
        public float DeathProbability;
        public SeedStates CurrentState;
        public Sprite Icon;
    }
    
    public class PlantItem : MonoBehaviour
    {
        public virtual string Key { get;}

        public SpriteRenderer Sprite;
        public BoxCollider2D BoxCollider2D;

        public Sprite CanHarvestIcon;

        public PlantData Data;

        public GridData GridData { get; set; }
        
        public List<PlantState> PlantStates => Data.PlantStates;
        public PlantState CurrentPlantState;
        public int PlantIndex = 0;
        public int mPlantCount = 0;
        
        public virtual SeedStates State { get; }
        

        /// <summary>
        /// 天数更改方法
        /// </summary>
        public virtual void ChangeDay()
        {
        }

        /// <summary>
        /// 被收获后显示种植物果实图片
        /// </summary>
        public virtual void ChangeHarvestIcon()
        {
        }

        public virtual void UpdatePlant()
        {
        }

        /// <summary>
        /// 植物死亡
        /// </summary>
        public virtual void PlantDead()
        {
            var deathProbability = Random.Range(0, 1f);
            if (deathProbability < CurrentPlantState.DeathProbability)
            {
                GridData.ResetGrid();
                MessagePanel.Default.AddMessage($"{Data.Key}死亡");
                this.DestroyGameObjGracefully();
            }
        }
    }
}